
#include "stat_primitives.h"

inline real __vsum(int m, real * vA, int inca) {

	int i;
	real s;
	s = 0.0;
	for (i = 0; i < m; i++) {
		s += *vA;
		vA += inca;
	}
	return s;
}

inline real __vmean(int m, real * vA, int inca) {

	real mean;
	mean = __vsum(m, vA, inca);
	mean = mean / m;
	return (double) mean;
}

inline void __msum(int m, int n, real * mA, int sta, real * vS, int incs) {

	int j;

	for (j = 0; j < n; j++) {
		*vS = __vsum(m, mA, sta);
		vS += incs;
		mA++;
	}
}

inline void __msum_row(int m, int n, real * mA, int sta, real * vS, int incs) {

	int i;

	for (i = 0; i < m; i++) {
		*vS = __vsum(n, mA, 1);
		vS += incs;
		mA += sta;
	}
}

inline void __mmean(int m, int n, real * mA, int sta, real * vM, int incm) {

	int i;

	for (i = 0; i < n; i++) {
		*vM = __vmean(m, mA, sta);
		mA++;
		vM += incm;
	}
}

inline void __mmean_row(int m, int n, real * mA, int sta, real * vM, int incm) {

	int i;

	for (i = 0; i < m; i++) {
		*vM = __vmean(n, mA, 1);
		mA += sta;
		vM += incm;
	}

}

inline real __vmax(int m, real * vA, int inca, real * max, int * id) {

	*max = *vA;
	int idx = 0;
	int i;
	for (i = 0; i < m; i++) {

		if (*vA > *max) {
			*max = *vA;
			*id = idx;
		}

		vA += inca;
		idx += inca;
	}
	return *max;
}

inline real __vmin(int m, real * vA, int inca, real * min, int * id) {

	*min = *vA;
	int idx = 0;
	int i;
	for (i = 0; i < m; i++) {

		if (*vA < *min) {
			*min = *vA;
			*id = idx;
		}

		vA += inca;
		idx += inca;
	}
	return *min;
}

inline void __vminmax(int m, real * vA, int inca, real * min, real * max) {

	*min = *vA;
	*max = *vA;
	int i;
	for (i = 0; i < m; i++) {

		if (*vA < *min) {
			*min = *vA;
		}
		if (*vA > *max) {
			*max = *vA;
		}

		vA += inca;
	}
}

inline void __vminmax_id(int m, real * vA, int inca, real * min, int * id_min, real * max, int * id_max) {

	*min = *vA;
	*max = *vA;
	int idx = 0;
	int i;
	for (i = 0; i < m; i++) {

		if (*vA < *min) {
			*min = *vA;
			*id_min = idx;
		}
		if (*vA > *max) {
			*max = *vA;
			*id_max = idx;
		}

		vA += inca;
		idx += inca;
	}
}

